<?php defined('SYSPATH') or die('No direct script access.');

/*
 * uniapp点评功能
 */

class Controller_Pc_Api_V2_Comment extends Controller_Pc_Api_v2_Base
{
    private $result;

    /**
     * 验证类
     * @var Request_Api_V2_Comment
     */
    private $validation;

    /**
     * 服务类
     * @var Service_Api_Comment
     */
    private $service;

    //评论api控制器
    public function before()
    {
        parent::before();
        $this->result = array(
            'status' => true,//默认状态为成功
            'msg' => '操作成功',//默认的提示消息
            'data' => array()
        );
        $this->validation = Request_Api_V2_Factory::create('comment', $this->request_params);
        $this->service = new Service_Api_Comment();

    }
    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }


    /**
     * 获取评论列表
     */
    public function action_list()
    {
        try {

            $this->validation->list()->check();
            //类型id
            $type_id = (int)$this->request_params['type_id'];
            //产品id
            $product_id = (int)$this->request_params['product_id'];
            //条数
            $pagesize = (int)$this->request_params['pagesize'];
            //页码
            $page = (int)$this->request_params['page'];
            //标识
            $flag = $this->request_params['flag'];
            $data = $this->service->list($type_id, $product_id, $flag, $page, $pagesize);
            $this->result['data'] = $data;

        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    //评论统计信息
    public function action_count_info()
    {
        try {

            $this->validation->stats_info()->check();
            //类型id
            $type_id = (int)$this->request_params['type_id'];
            //产品id
            $product_id = (int)$this->request_params['product_id'];
            $data = $this->service->stats_info($product_id, $type_id);
            $this->result['data'] = $data;

        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }


    // 添加用户评论
    public function action_add_comment()
    {
        try {
            $db = Database::instance();
            $db->begin();
            $this->validation->add_comment()->check();
            //类型id
            $flag = $this->service->add_order_comment($this->request_params);
            if ( ! $flag) {
                $this->result['status'] = false;
                $this->result['msg'] = '添加失败';
            }
            $db->commit();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage().$e->getTraceAsString());
            $db->rollback();
        }

    }

    //文章评论
    public function action_add_article_comment_save()
    {
        try {
            $this->validation->add_article_comment()->check();
            //类型id
            $rtn = $this->service->add_article_comment($this->request_params);
            if ( ! count($rtn)) {
                $this->result['status'] = false;
                $this->result['msg'] = '添加失败';
            } else {
                $this->result['data'] = $rtn;
            }

        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }


    }


}